package com.zbs.simbot.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;

@Slf4j
public class FileUtil {

    private FileUtil() {
    }

    public static boolean createDirectory(String filePath) {
        filePath = filePath.replaceAll("\\\\", "/").replaceAll("//", "/");
        String[] split = filePath.split("/");
        //最后一个就是文件名
        String fileName = split[split.length - 1];
        File file;
        boolean flag = true;
        if (fileName.contains(".")) {
            String directoryPath = filePath.replace(fileName, "");
            file = new File(directoryPath);
        } else {
            file = new File(filePath);
            flag = false;
        }
        if (!file.exists()) {
            file.mkdirs();
        }
        return flag;
    }

    public static File createNewFile(String filePath) {
        if (!StringUtils.hasLength(filePath)) {
            return null;
        }
        // 创建文件，先创建文件夹
        boolean directory = createDirectory(filePath);
        if (!directory) {
            return null;
        }

        File file = new File(filePath);
        //如果文件不存在则新建
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                log.error("创建文件出错：{}", e.getMessage());
                log.error("创建文件出错:", e);
                return null;
            }
        }
        return file;
    }

    public static boolean deleteFile(String filePath) {
        if (!StringUtils.hasLength(filePath)) {
            return false;
        }
        filePath = filePath.replaceAll("\\\\", "/").replaceAll("//", "/");
        File file = new File(filePath);
        //如果文件存在则删除
        if (file.exists()) {
            return file.delete();
        }
        return true;
    }

    public static boolean deleteMasterFileAndSubfile(String filePath) {
        if (!StringUtils.hasLength(filePath)) {
            return false;
        }
        filePath = filePath.replaceAll("\\\\", "/").replaceAll("//", "/");
        File directory = new File(filePath);
        if (directory.exists()) {
            return deleteMasterFileAndSubfile(directory);
        }
        return false;
    }

    public static boolean deleteHomeDirectoryAndSubdirectory(String directoryPath) {
        if (!StringUtils.hasLength(directoryPath)) {
            return false;
        }
        directoryPath = directoryPath.replaceAll("\\\\", "/").replaceAll("//", "/");
        File directory = new File(directoryPath);
        if (directory.exists()) {
            return deleteHomeDirectoryAndSubdirectory(directory.toPath());
        }
        return false;

    }

    private static boolean deleteHomeDirectoryAndSubdirectory(Path directory) {
        try {
            Files.walk(directory)
                    .sorted(Comparator.reverseOrder())
                    .map(Path::toFile)
                    .forEach(File::delete);
        } catch (IOException e) {
            log.error("删除文件出错：{}", e.getMessage());
            log.error("删除文件出错:", e);
            return false;
        }
        return true;
    }

    private static boolean deleteMasterFileAndSubfile(File directory) {
        try {
            Files.walk(directory.toPath())
                    .filter(Files::isRegularFile)
                    .map(Path::toFile)
                    .forEach(File::delete);
        } catch (IOException e) {
            log.error("删除文件出错：{}", e.getMessage());
            log.error("删除文件出错:", e);
            return false;
        }
        return true;
    }

}
